package com.example.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Exam;

@Mapper
public interface ExamMapper extends BaseMapper<Exam> {
    
    @Select("SELECT * FROM exams WHERE creator_id = #{creatorId}")
    List<Exam> findByCreatorId(@Param("creatorId") Long creatorId);
    
    @Select("SELECT e.* FROM exams e " +
            "JOIN exam_classes ec ON e.id = ec.exam_id " +
            "WHERE ec.class_id = #{classId}")
    List<Exam> findByClassId(@Param("classId") Long classId);
    
    @Select("SELECT e.* FROM exams e " +
            "JOIN exam_classes ec ON e.id = ec.exam_id " +
            "JOIN class_students cs ON ec.class_id = cs.class_id " +
            "WHERE cs.student_id = #{studentId} AND cs.status = 'active'")
    List<Exam> findByStudentId(@Param("studentId") Long studentId);
    
    @Update("UPDATE exams SET published = 1, publish_time = NOW() WHERE id = #{examId}")
    int publishExam(@Param("examId") Long examId);
} 